package com.hzqy.web.ac.epg;

import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.TokenkeyUtil;
import com.hzqy.service.epg.epgCommonService;
import com.hzqy.web.login.SessionKey;


@Controller
@RequestMapping("/")
public class epgCommonAction {
	@Resource
	private epgCommonService epgCommonService;
	/**
	 * 返回各个产品类型订购状态 --
	 * 注意：目前英超和NBA没有加入接口，如果有相关产品需要进行产品鉴权
	 * result --> 0订购 -- 504未订购 --error 查询产品失败，接口未响应
	 * 		kid_month--少儿包月
	 kid_ThirdLine --少儿线下第三方
	 mv_month--电影包月
	 mv_ThirdLine--电影线下第三方
	 tv_month--电视剧包月
	 tv_ThirdLine--电视剧线下第三方
	 mtv_month--影视剧包月
	 mtv_ThirdLine--影视剧线下第三方
	 filmAll--合家欢
	 NBA--NBA
	 YC--英超
	 nba_yc-- NBA和英超
	 */
	@RequestMapping(value="/epgAuthCommon")
	@ResponseBody
	public Object acLotterySelect(String userid,HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> map = epgCommonService.getOrderType(userid,request,response);
		return map;
	}

	/**
	 * 生成固定随机数-----------
	 * 传入需要的随机数数组
	 */
	public static int randomNum(int[] num) {
		int lengthNum = num.length;
		if(lengthNum == 0) {
			return -1;
		}
		SecureRandom random = new SecureRandom();
		int n = (int) (random.nextDouble()* lengthNum);
		return num[n];
	}
	/**
	 * 加密验证
	 * @param userID  iptv账号
	 * @return
	 */
	public static String token(String userID) {
		String tokenkey = TokenkeyUtil.getTokenkey("zjgditv", userID, "super$^@%!*", "44activity");
		return tokenkey;
	}

	/**
	 * 生成四位随机数
	 * @param avu
	 * @return
	 */
	@RequestMapping(value="/getRandom")
	@ResponseBody
	public Object randomFour(HttpSession session){
		Map<String,Object> map=new HashMap<String, Object>();
		SecureRandom random = new SecureRandom();
		int i = (int) ((random.nextDouble() * 9 + 1) * 1000);
		map.put("rand",i);
		String userid= String.valueOf( session.getAttribute(SessionKey.SESSION_SERVERID));
		session.setAttribute(userid+"random", i);
		return map;
	}
	/**
	 * 计算当前日期与指定日期相查天数
	 * @param time1 指定日期 20190820
	 * @return
	 */
	@RequestMapping(value="/getDataCount")
	@ResponseBody
	public Object getDataCount(String time){
		Map<String,Object> map=new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int count = 0;
		try {
			count = differentDays(sdf.parse(time),new Date());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		map.put("result", count);
		return map;
	}
	/**
	 * date2比date1多的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDays(Date date1,Date date2)
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);

		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if(year1 != year2) {  //同一年
			int timeDistance = 0 ;
			for(int i = year1 ; i < year2 ; i ++) {
				if(i%4==0 && i%100!=0 || i%400==0) {   //闰年
					timeDistance += 366;
				}
				else {   //不是闰年
					timeDistance += 365;
				}
			}
			return timeDistance + (day2-day1) ;
		}else {   //不同年
			return day2-day1;
		}
	}
	/**
	 * 加密验证
	 * @param userID  iptv账号
	 * @return
	 */
	public static String getEPGSecretKey(String userID) {
		String tokenkey = TokenkeyUtil.getEPGSecretKey("KDactivity"+userID+"secret!@#$4321");
		System.out.println(userID+"密钥:"+tokenkey);
		return tokenkey;
	}
}
